package stream;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * Stream终止操作
 *
 * @author <a href="mailto:sjj@jianzhimao.com">mojo</a>
 * copyright (C), 2013-2022, 广州九尾信息科技有限公司
 * @date 2022/4/11 11:00
 */
public class Stream03 {
    public static void main(String[] args) {
        //规约操作Collectors
        List<Apple> apples = getApples();
        //toList(apples);
        //toMap(apples);
        //testStreamAndParallelStream();
        //testSummaryStatistics();
        testPeek(apples);
    }

    private static void testPeek(List<Apple> apples) {
        List<Apple> collect = apples.stream().filter(apple -> apple.getWeight() < 20).peek(apple -> System.out.println(">>>" + apple)).collect(Collectors.toList());
    }


    private static void testSummaryStatistics() {
        //获取数字的个数、最小值、最大值、总和以及平均值
        List<Integer> primes = Arrays.asList(2, 3, 1, 5, 7, 11, 13, 17, 19, 23, 29);
        IntSummaryStatistics stats = primes.stream().mapToInt((x) -> x).summaryStatistics();
        System.out.println("Highest prime number in List : " + stats.getMax());
        System.out.println("Lowest prime number in List : " + stats.getMin());
        System.out.println("Sum of all prime numbers : " + stats.getSum());
        System.out.println("Average of all prime numbers : " + stats.getAverage());


    }

    private static void testStreamAndParallelStream() {
        long t0 = System.nanoTime();

        //初始化一个范围100万整数流,求能被2整除的数字，toArray()是终点方法

        int a[] = IntStream.range(0, 100000).filter(p -> p % 2 == 0).toArray();

        long t1 = System.nanoTime();

        //和上面功能一样，这里是用并行流来计算

        int b[] = IntStream.range(0, 100000).parallel().filter(p -> p % 2 == 0).toArray();

        long t2 = System.nanoTime();

        //我本机的结果是serial: 0.06s, parallel 0.02s，证明并行流确实比顺序流快

        System.out.printf("serial: %.2fs, parallel %.2fs%n", (t1 - t0) * 1e-9, (t2 - t1) * 1e-9);

    }

    private static void toMap(List<Apple> apples) {
        Map<String, Integer> appleMap = apples.stream().collect(Collectors.toMap(Apple::getColour, Apple::getWeight, (v1, v2) -> v1));
        Set<Map.Entry<String, Integer>> entries = appleMap.entrySet();
        for (Map.Entry<String, Integer> entry : entries) {
            System.out.println(entry.getKey() + ">>" + entry.getValue());

        }
    }

    private static void toList(List<Apple> apples) {
        List<String> collect = apples.stream().map(apple -> apple.getColour() + "_" + apple.getWeight()).collect(Collectors.toList());
        collect.forEach(System.out::println);
    }

    private static List<Apple> getApples() {
        List<Apple> apples = new ArrayList<>();
        apples.add(new Apple("红色", 111, true));
        apples.add(new Apple("黄色22", 222, true));
        apples.add(new Apple("蓝色333", 333, true));
        apples.add(new Apple("红色", 1111, false));
        apples.add(new Apple("黄色4444", 222, false));
        apples.add(new Apple("蓝色", 333, false));
        apples.add(new Apple("粉色", 444, false));
        return apples;
    }
}
